<?php
    
    include("sessiontest.php");
    
    include("classes/xmlrpc.inc.php");
    include("classes/rtorrent.cls.php");
    include("classes/torrent.cls.php");
    
    $rTorrent = new rTorrent();
    $torrents = $rTorrent->getTorrents();
    
    $active = array();
    $seeding = array();
    $completed = array();
    
    foreach ($torrents as $torrent) {
      $state = $torrent->getState();
      if ($state == -1 || $state == 0)
        $completed[] = $torrent;
      elseif ($state == 1 || $state == 3)
        $active[] = $torrent;
      elseif ($state == 2)
        $seeding[] = $torrent;
    }
    
?>

<div id="torrents" title="Torrents" class="panel">

    <ul class="menu">
      <li><a href="#active" selected="true" target="_view">Active</a></li>
      <li><a href="#seeding" target="_view">Seeding</a></li>
      <li><a href="#completed" target="_view">Completed</a></li>
    </ul>
    
    <div id="viewer">
    
      <fieldset id="active">
        <?php
          if (count($active) > 0) {
            foreach ($active as $torrent) {
              echo "<div class=\"row\">";
              echo "<a href=\"details.php?hash=" . $torrent->getHash() . "\">";
              echo "<span class=\"percentage\">" . $torrent->getPercentage() . "%</span>";
              echo "<span class=\"title\">" . $torrent->getName() . "</span>";
              echo "<span class=\"speed\"";
              echo "<span class=\"download\">" . $torrent->getDownloadRate() . "</span>";
              echo "<span class=\"upload\">" . $torrent->getUploadRate() . "</span>";
              echo "</span>";
              echo "</a>";
              echo "</div>";
            }
          }
          
          else {
            echo "<div class=\"row\">";
            echo "<p class=\"notice\">No Active Torrents</p>";
            echo "</div>";
          }
        ?>
      </fieldset>
      
      <fieldset id="seeding" style="display: none;">
        <?php
          if (count($seeding) > 0) {
            foreach ($seeding as $torrent) {
              echo "<div class=\"row\">";
              echo "<a href=\"details.php?hash=" . $torrent->getHash() . "\">";
              echo "<span class=\"percentage\">" . $torrent->getPercentage() . "%</span>";
              echo "<span class=\"title\">" . $torrent->getName() . "</span>";
              echo "<span class=\"speed\"";
              echo "<span class=\"download\">" . $torrent->getDownloadRate() . "</span>";
              echo "<span class=\"upload\">" . $torrent->getUploadRate() . "</span>";
              echo "</span>";
              echo "</a>";
              echo "</div>";
            }
          }
          
          else {
            echo "<div class=\"row\">";
            echo "<p class=\"notice\">No Seeding Torrents</p>";
            echo "</div>";
          }
        ?>
      </fieldset>
      
      <fieldset id="completed" style="display: none;">
        <?php
          if (count($completed) > 0) {
            foreach ($completed as $torrent) {
              echo "<div class=\"row\">";
              echo "<a href=\"details.php?hash=" . $torrent->getHash() . "\">";
              echo "<span class=\"percentage\">" . $torrent->getPercentage() . "%</span>";
              echo "<span class=\"title\">" . $torrent->getName() . "</span>";
              echo "<span class=\"speed\"";
              echo "<span class=\"download\">" . $torrent->getDownloadRate() . "</span>";
              echo "<span class=\"upload\">" . $torrent->getUploadRate() . "</span>";
              echo "</span>";
              echo "</a>";
              echo "</div>";
            }
          }
          
          else {
            echo "<div class=\"row\">";
            echo "<p class=\"notice\">No Completed Torrents</p>";
            echo "</div>";
          }
        ?>
      </fieldset>
    </div>

</div>

<div id="tabs">
  
</div>